/**
 * 建委官网工程中标公告
 *  
 */
import { Base, Item } from '../base';
import { timeout } from 'q';
import { ENGINE_METHOD_ALL } from 'constants';
class jwgwzb extends Base {
    constructor() {
        super();
        this.name = '沈阳城乡建委官网(工程中标公告)';
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "lnxm";
        this.collectionShowName = "辽宁项目爬虫";
        this.keyField = "title";
    }
    getItems(pageIndex: number): Item[] {
        let num = pageIndex + 1;
        let url = `http://www.syjs.gov.cn/ztb/3/0/${num}.aspx`;
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        console.log(url);
        let result: Item[] = [];
        let that = this;
        let first = $('.msgtable').eq(1).find('tr').first();
        try {
            $('.msgtable').eq(1).find('tr').not(first).each(function (index, ele) {
                let item = that.buildItem();
                let title = $(ele).find('td').eq(0).find('a').attr('title').trim();
                let url = $(ele).find('td').eq(0).find('a').attr('href');

                result.push(that.getExtraItem(url, title));
            });
        } catch (error) {
            console.error(error);
        }
        return result;
    }
    getExtraItem(url: string, title: string): Item {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        let first = $('#_Sheet1 tr').eq(0);
        let item = this.buildItem();
        let that =this;
        $('#_Sheet1 tr').not(first).each(function (index, ele) {
            let l = $(ele).find('td').length;
            if (l == 2) {
                (<any>item)[$(ele).find('td').eq(0).text().trim()] = $(ele).find('td').eq(1).text().trim();
            } else {
                (<any>item)[$(ele).find('td').eq(0).text().trim()] = $(ele).find('td').eq(1).text().trim();
                (<any>item)[$(ele).find('td').eq(2).text().trim()] = $(ele).find('td').eq(3).text().trim();
            }
            item.title=title;
            item.source=that.name;
            (<any>item).url=url;
        });
        return item;
    }
    getPageCount(): number {
        return 1348;
    }
}
module.exports = new jwgwzb();